package project.android;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class UserTaskPage extends ListActivity{

	
	int id;
	String name=new String();
    String members=new String();
	String tasks=new String();
	String guids;
	static String[] LIST;
	static String[] taskGuid;
	Context context;
	
	
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        final ListView lv = getListView(); 
        lv.setTextFilterEnabled(true);
        context = this;
		
        //get guid of the current group
        Bundle extras = getIntent().getExtras(); 
        guids = extras.getString("GROUPGUID")+"*"+extras.getString("USERGUID");
      
        Button addTaskButton = new Button(this);
        addTaskButton.setText("Add New Task");
        addTaskButton.setOnClickListener(new View.OnClickListener() {
        	
            public void onClick(View v) {
            	
            	LayoutInflater factory = LayoutInflater.from(UserTaskPage.this);
    	        final View input = factory.inflate(R.layout.new_task_to_user, null);
    	        new AlertDialog.Builder(UserTaskPage.this)
    	        	.setView(input)
    	        	.setPositiveButton("Save", 
    	        		new DialogInterface.OnClickListener() {
    			        	public void onClick(DialogInterface dialog, int whichButton){
    			        		

    			        		String toBeSend = guids+"*";
    			        		
    			        		//assign the inputs to variables
    			        		toBeSend += ((EditText) input.findViewById(R.id.nameIn1)).getText().toString()+"*";
    			        		toBeSend += ((EditText) input.findViewById(R.id.descriptionIn1)).getText().toString()+"*"; 
    			        		toBeSend += ((EditText) input.findViewById(R.id.deadlineIn1)).getText().toString();
    			        				
    			        		try{
    			                	Synchronized.syncGroups(new DatabaseConnection(context),toBeSend,"tasks/addTaskToAnotherUser");
    			                }
    			                catch(Exception e){}
    			        		
    			        		finish();
    			                
    			        	} 	
    	        	})
    		        .setNeutralButton(android.R.string.cancel,
    		            new DialogInterface.OnClickListener() {
    			           public void onClick(DialogInterface dialog, int whichButton){
    			        	   
    			        	   finish();
    			           
    			           }
    		        })
    		        .show();
            	
            }
        });
        
        lv.addFooterView(addTaskButton);
        
        
        String[] groupInfo=null;
        
        //connect to server to get the information of the current group
        try{
        	groupInfo = Synchronized.syncGroups(new DatabaseConnection(this),guids,"groups/getusertasks");
        }
        catch(Exception e){
        	new AlertDialog.Builder(this)
        	.setTitle("Server Connection Error")
        	.setMessage(e.getStackTrace().toString())
	        .setNeutralButton(android.R.string.cancel,
	            new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   finish();
		           }
	        })
	        .show();
        }
        
        if(groupInfo != null){
          	LIST = new String[groupInfo.length-1];
        	taskGuid = new String[groupInfo.length-1];
        	for(int i = 0;i<groupInfo.length-1;i++){
        		String[] n = groupInfo[i].split("~");
        		LIST[i] = new String();
        		taskGuid[i]=new String();
        		LIST[i] = n[1];
        		taskGuid[i] = n[0];		
        	}
            
        }
        else{
        	
        	LIST = new String[1];
        	LIST[0] = "This user doesn't have any task!";
        	
        }
        
      //create onclick event for each list item
		lv.setOnItemClickListener(new OnItemClickListener() { 
        	public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
        		Intent i = new Intent(UserTaskPage.this, CurrentTaskPage.class);
        		int j;
        		for(j = 0; j < LIST.length;j++)
        			if(lv.getItemAtPosition(position).toString()==LIST[j]) break;
        		i.putExtra("TASKGUID", taskGuid[j]);
        		startActivity(i);
        	} 
        }); 
	
	//set list view
    setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item, LIST));
        
        
    }
    
}


